package cn.ichiva.tushare.sdk.tushare;

import cn.ichiva.tushare.sdk.common.HttpUtils;
import cn.ichiva.tushare.sdk.tushare.entity.TuRequest;
import cn.ichiva.tushare.sdk.tushare.entity.TuResponse;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;

/**
 * 客户端
 */
@Slf4j
public class TuClient {

    private String url;
    private String token;

    public TuClient(String url, String token) {
        this.url = url;
        this.token = token;
    }


    public Optional<TuResponse> post(TuRequest request){
        request.setToken(token);
        try {
            String json = HttpUtils.doPost(url, JSON.toJSONString(request.toQuery()));
            TuResponse tuResponse = JSON.parseObject(json, TuResponse.class);
            if(null != tuResponse) return Optional.of(tuResponse);
        } catch (Exception e) {
            log.warn("拉取数据失败 api = " + request.getApi(),e);
        }
        return Optional.empty();
    }

}
